package com.passion.web.alone;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.passion.common.base.ResultVO;
import com.passion.context.BizServiceBeanContext;
import com.passion.frm.UserLoginFrm;
import com.passion.vo.LoginUser;
import com.passion.vo.UserLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 类描述：用户登录控制器
 *
 * @author ZhuYin
 * @since 2024年11月19日
 */
@Slf4j
@Api(tags = "用户登录")
@RestController
@RequestMapping("/sys/user")
public class LoginController {

    @ApiOperationSupport(author = "ZhuYin", order = 10)
    @ApiOperation("用户登录")
    @PostMapping("/doLogin")
    public ResultVO<UserLoginVO> doLogin(@RequestBody @Validated UserLoginFrm frm) {
        LoginUser loginUser = BizServiceBeanContext.loginService.doLogin(frm);
        // 登录成功
        StpUtil.login(loginUser.getId(), true);
        StpUtil.getTokenSession().set("user", loginUser);
        // token信息
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        // 构建返回VO对象
        UserLoginVO userLoginVO = new UserLoginVO();
        userLoginVO.setLoginUser(loginUser);
        userLoginVO.setTokenInfo(tokenInfo);
        // 发送用户登录MQ
        BizServiceBeanContext.businessMqSender.userLoginMq(loginUser);
        return ResultVO.getSuccess(userLoginVO);
    }


}
